﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace F3.Maps.OsmParser
{
    public abstract class NodeBase
    {
        public XElement Element { get; private set; } 
        
        private IEnumerable<XElement> tags = null;

        public NodeBase(XElement element)
        {
            this.Element = element;
            this.ID = Attribute("id");

        }
        
        public IEnumerable<XElement> GetTags()
        {
            if (tags == null)
            {
                tags = this.Element.Elements("tag");
            }
            return tags;
        }

        public string GetTag(string key)
        {
                                       
            var tag = GetTags().Where((element) => element.Attribute("k").Value == key).FirstOrDefault();
            return tag != null ? tag.Attribute("v").Value.ToString() : string.Empty;
        }

        public string Attribute(string attribute)
        {
            return this.Element.Attribute(attribute).Value.ToString();
        }

        public string ID { get; protected set; }
    }
}
